package com.zhht.checkcop.parser;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.zhht.checkcop.db.StaticDBMap;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class Parser88dusNovel implements Pipeline{
	
	private String novelUrl; 
	
	private String type;
	public Parser88dusNovel(){}
	public Parser88dusNovel(String type,String novelUrl){
		this.type = type;
		this.novelUrl = novelUrl;
	}
	public void process(ResultItems resultItems, Task arg1) {
		Map<String, Object> mapResults = resultItems.getAll();
        Iterator<Entry<String, Object>> iter = mapResults.entrySet().iterator();
        Map.Entry<String, Object> entry;
        
        List<String> name=new ArrayList<String>();
        List<String> url=new ArrayList<String>();
        List<String> zj=new ArrayList<String>();
        List<String> zz=new ArrayList<String>();
        List<String> zs=new ArrayList<String>();
        List<String> sj=new ArrayList<String>();
        List<String> zt=new ArrayList<String>();
        List<String> fs=new ArrayList<String>();
        String typeName="";
        
        // 输出到控制台
        while (iter.hasNext()) {
            entry = iter.next();
            if(entry.getKey().equals("name")){
            	name.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("url")){
            	url.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("zj")){
            	zj.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("zz")){
            	zz.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("zs")){
            	zs.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("sj")){
            	sj.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("zt")){
            	zt.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("fs")){
            	fs.addAll((Collection<? extends String>) entry.getValue());
            }
            if(entry.getKey().equals("typeName")){
            	typeName = entry.getValue() + "";
            }
        }
        
        if(null!=name&&name.size()>0){
        	SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        	for(int i=0;i<name.size();i++){
        		Map<String,Object> paramMap=new HashMap<String, Object>();
        		paramMap.put("classify_id", type);
        		paramMap.put("classify_name", typeName);
        		paramMap.put("name", name.get(i));
        		paramMap.put("source_url", novelUrl+url.get(i));
        		paramMap.put("last_catalog", zj.get(i));
        		paramMap.put("author_name", zz.get(i));
        		paramMap.put("word_count", zs.get(i).replace("字", ""));
        		paramMap.put("create_date", sdf.format(new Date()));
        		paramMap.put("update_date", sj.get(i));
        		paramMap.put("status", zt.get(i).trim().equals("连载中")?"1":"2");
        		paramMap.put("fs", fs.get(i));
        		
//        		Map<String, Object> exitMap = StaticDBMap.existNovelNameMap.get(name.get(i)+","+zz.get(i)+",");
        		Map<String, Object> exitMap = StaticDBMap.existNovelNameMap.get(novelUrl+url.get(i));
        		
        		if(null == exitMap){
        			StaticDBMap.writeNovelNameMap.put(name.get(i)+","+zz.get(i), paramMap);
        		}else{
        			StaticDBMap.updateExistNovelNameMap.put(exitMap.get("id")+"", paramMap);
        		}
        	}
        }
	}

}
